<?php
error_reporting(E_ALL);
set_time_limit(0);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
require_once  './Classes/PHPExcel.php';

include_once("./config.php");

$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8mb4")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}

$check_query = $mysqli->query("select * from users");
$user_list=array();
while ($list = $check_query->fetch_array()){
	$user_list[]=array('gender'=>$list['gender'],'name'=>$list['name'], 'yue'=>$list['yue'], 'ri'=>$list['ri'],
			'phone'=>$list['phone'],  'address'=>$list['address'], 'bao'=>$list['bao'],'create_time'=>$list['create_time'] );
}
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '时间')
->setCellValue('B1', '性别')
->setCellValue('C1', '姓名')
->setCellValue('D1', '生日')
->setCellValue('E1', '手机号')
->setCellValue('F1', '地址')
->setCellValue('G1', '保单号')
;
$i=2;
foreach ($user_list as $v){
	if('man' == $v['gender']){$gender='男';}else{$gender='女';}
	$shengri=NULL;
	if(strlen($v['yue']) >1 ){
		$shengri=$v['yue'];
	}else{
		$shengri='0'.$v['yue'];
	}
	$shengri.='月/';
	if(strlen($v['ri']) >1 ){
		$shengri.= $v['ri'];
	}else{
		$shengri.= '0'.$v['ri'];
	}
	$shengri.= '日';
    $objPHPExcel->setActiveSheetIndex(0)
		 ->setCellValue('A'.$i, $v['create_time'])
         ->setCellValue('B'.$i, $gender)
         ->setCellValue('C'.$i, $v['name'])
         ->setCellValue('D'.$i, $shengri)
	     ->setCellValue('E'.$i, $v['phone'])
		 ->setCellValue('F'.$i, $v['address'])
		 ->setCellValue('G'.$i, $v['bao'])
    ;
	$i++;
}       


$mysqli->close();

$file='周生生('.date("Y-m-d").').xlsx';
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename='.$file);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');




